What is decamelize?
The decamelize npm package is used to convert a string from camelCase to a lower case string with a custom separator (defaulting to an underscore). It is useful for transforming variable names or keys in objects from camelCase to a format that is more common in other languages or contexts, such as snake_case for database fields or kebab-case for CSS class names.
What are decamelize's main functionalities?
Decamelize a string
Converts 'myCamelCaseString' to 'my_camel_case_string' using the default separator.
"myCamelCaseString".decamelize()
Decamelize with a custom separator
Converts 'myCamelCaseString' to 'my-camel-case-string' using a custom separator ('-').
"myCamelCaseString".decamelize('-')
Preserve consecutive uppercase characters
Converts 'myURLString' to 'my_url_string', preserving the consecutive uppercase characters.
"myURLString".decamelize('_', { preserveConsecutiveUppercase: true })
Other packages similar to decamelize
snake-case
The snake-case package converts strings to snake_case. It is similar to decamelize but is specifically tailored to produce snake_case without providing options for custom separators.
kebab-case
The kebab-case package is designed to convert strings to kebab-case. While it serves a similar purpose in changing the case of strings, it is focused on kebab-case rather than providing a customizable separator.
change-case
The change-case package is a more comprehensive string transformation library that includes a variety of case conversion functions, including decamelize. It offers a broader set of functionalities compared to the single-purpose decamelize package.
decamelize
Convert a camelized string into a lowercased one with a custom separator
Example: unicornRainbow
→ unicorn_rainbow
If you use this on untrusted user input, don't forget to limit the length to something reasonable.
Install
$ npm install decamelize
Usage
const decamelize = require('decamelize');
decamelize('unicornRainbow');
decamelize('unicornRainbow', {separator: '-'});
decamelize('testGUILabel', {preserveConsecutiveUppercase: true});
decamelize('testGUILabel', {preserveConsecutiveUppercase: false});
API
decamelize(input, options?)
input
Type: string
options
Type: object
separator
Type: string
Default: '_'
Character or string inserted to separate words in string
.
cosnt decamelize = require('decamelize');
decamelize('unicornRainbow');
decamelize('unicornRainbow', {separator: '-'});
preserveConsecutiveUppercase
Type: boolean
Default: false
Preserve sequences of uppercase characters.
const decamelize = require('decamelize');
decamelize('testGUILabel');
decamelize('testGUILabel', {preserveConsecutiveUppercase: true});
Related
See camelcase
for the inverse.